package com.scheduled;

import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jd.api.ProductInfoFindClient;
import com.mrobuy.jdvc.dao.JDAccountDao;
import com.mrobuy.jdvc.dao.VCCateBrandDao;
import com.mrobuy.jdvc.dao.VCProductInfoDao;
import com.mrobuy.jdvc.entity.JDAccount;
import com.mrobuy.jdvc.entity.VCCateBrand;
import com.mrobuy.jdvc.entity.VCProductInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 商品状态信息同步
 *
 */
@Component
public class ProductInfoSyncTask {

    private static Logger LOG = LoggerFactory.getLogger(ProductInfoSyncTask.class);

    @Autowired
    private JDAccountDao jdAccountDao;

    @Autowired
    private VCCateBrandDao vcCateBrandDao;

    @Autowired
    private VCProductInfoDao vcProductInfoDao;

    private final AtomicBoolean running = new AtomicBoolean(false);

    // @Scheduled(cron="* 0 0 2 * * ?")
    @Scheduled(fixedRate = 1000*60*60)
    public void productStateSync() {

        if ( !TaskSwitch.PRODUCT_INFO_SYNC.get() ) {
            return;
        }

        if ( running.compareAndSet(false, true) ) {
            List<JDAccount> accounts = jdAccountDao.list();
            if ( accounts!=null ) {
                for (JDAccount account : accounts) {
                    boolean sync = "Y".equalsIgnoreCase(account.getAutoSync());
                    LOG.info("[我的商品列表][{}]供应商简码: {}; 自动同步: {}", account.getName(), account.getVccode(), sync);
                    if ( sync ) {
                        exec(account);
                    }
                }
            }
            running.set(false);
        }
    }

    private void exec(JDAccount account) {

        List<VCCateBrand> vcCateBrands =  vcCateBrandDao.list(account.getVccode());
        if ( vcCateBrands==null || vcCateBrands.size()==0 ) {
            return;
        }

        JdClient client = new DefaultJdClient(JdClientFactory.serverUrl, account.getAccess_token(), account.getAppkey(), account.getAppsecret());

        ProductInfoFindClient productInfoFindClient = Jos.productInfoFindClient(client);

        for (VCCateBrand cateBrand : vcCateBrands) {
            try {
                List<VCProductInfo> productInfos = productInfoFindClient
                        .setCategoryId(cateBrand.getCateid())
                        .setBrandId(cateBrand.getBrandid())
                        .call();

                LOG.info("" + cateBrand.getCatename() + ", " + cateBrand.getName() + " ==> " + productInfos.size());

                for (VCProductInfo productInfo : productInfos) {
                    productInfo.setVendorCode(account.getVccode());
                    VCProductInfo vcProductInfo = vcProductInfoDao.load(productInfo.getWareId());
                    if ( vcProductInfo==null ) {
                        vcProductInfoDao.insert(productInfo);
                    } else {
                        vcProductInfoDao.update(productInfo, false);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
}
